<?php

class Application_Model_Package extends Zend_Config_Xml {

    private $invalidXML = 0;
    private $packageFile;
    private $packagesPath;
    private $assetsPath;
    private $installed;
    private $installedVersion;
    private $asset;
    private $hubs = array();

    public function __construct($xml, $path, $package, $assetsPath) {
        $this->packageFile = $package;
        $this->packagesPath = $path;
        $this->assetsPath = $assetsPath;
        $this->init($xml);
    }

    private function init($xml, $section = null, $options = false) {
        try {
            parent::__construct($xml, $section, $options);

            /*
             * @todo: extract assets here and do the validation
             */
            $image = $this->getImageName();
            if ($image) {
                $assetsPdir = $this->assetsPath . "/" . $this->agent->id;
                ob_start();
                system("mkdir -p $assetsPdir");
                $this->asset = $assetsPdir . "/" . $image;
                $packagesPath = $this->packagesPath . "/" . $this->packageFile;
                system("tar xf $packagesPath -C $assetsPdir $image");
                $ob = ob_get_contents();
                ob_end_clean();
            }
        } catch (Exception $e) {
            $this->invalidXML = 1;
        }
    }

    public function setPackageFile($file) {
        $this->packageFile = $file;
    }

    public function setAssetsPath($val) {
        $this->assetsPath = $val;
    }

    public function setInstalled($data) {
        $this->installed = $data;
    }

    private function getImageName() {
        if ($this->package->image) {
            return $this->package->image;
        } else if ($this->agent->image) {
            return $this->agent->image;
        } else {
            return 0;
        }
    }

    public function getImage() {
        if (file_exists($this->asset)) {
            return $this->getImageName();
        } else {
            return 0;
        }
    }

    public function setInstalledVersion($data) {
        $this->installedVersion = $data;
    }

    public function convertToArray() {
        $allArray = $this->toArray();
        foreach ($allArray['package'] as $key => $value) {
            if ($key == 'image') {
                if ($this->getImage()) {
                    $allArray['agent'][$key] = $value;
                } else {
                    unset($allArray['agent'][$key]);
                }
            } else {
                $allArray['agent'][$key] = $value;
            }
        }
        unset($allArray['package']);
        if (array_key_exists('hubs', $allArray['agent'])) {
            if (count($allArray['agent']['hubs']['hub'])) {
                var_dump(count($allArray['agent']['hubs']));
                if(count($allArray['agent']['hubs']['hub']) == 1) {
                    array_push($this->hubs, $allArray['agent']['hubs']['hub']);
                    var_dump($this->hubs);
                } else {
                    $this->hubs = $allArray['agent']['hubs']['hub'];
                }
            }
        }
        unset($allArray['agent']['hubs']);
        $allArray['agent']['installed'] = $this->installed;
        $allArray['agent']['installedVersion'] = $this->installedVersion;
        return $allArray['agent'];
    }

    public function hasHubs() {
        if (count($this->hubs)) {
            return 1;
        } else {
            return 0;
        }
    }

    public function getHubs() {
        return $this->hubs;
    }

    public function isValid() {
        /*
         * @todo: return valis state for package
         */
        return 1;
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
